package com.blog.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.blog.entity.RegexUtil;
import com.blog.entity.User;
import com.blog.service.UserService;
import com.blog.listener.FileUploadUtil;


@Controller
public class UserController {
	
	@Autowired UserService userService;

	@RequestMapping("register")
	public String register(HttpServletRequest request){
		return "register";
	}
	@RequestMapping("center")
	public String center(HttpServletRequest request){
		return "center";
	}
	@RequestMapping("changePW")
	public String changePW(HttpServletRequest request){
		return "editPW";
	}
	@RequestMapping("changePic")
	public String changePic(HttpServletRequest request){
		return "editPic";
	}
	@RequestMapping("editinfo")
	public String editinfo(HttpServletRequest request){
		return "editinfo";
	}
	
	@RequestMapping("register.do")
	@ResponseBody 
	public JSONObject userRegister(@RequestBody User user){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		user.setRegisterTime(df.format(new Date()));// new Date()为获取当前系统时间
		if(user.getPic() == null){
			user.setPic("assets/images/userpic.png");
		}
		JSONObject resObject = new JSONObject();
		int res = 0;String rerro = "";
		boolean isPwd = RegexUtil.isPwd(user.getPassword());
		boolean isMobile = RegexUtil.isMobile(user.getPhone());
		boolean isDate = RegexUtil.isDate(user.getBornD());
		if(!isPwd){
			rerro = "密码格式不正确！";
			resObject.put("resMsg", rerro);
		}else if(!isMobile){
			rerro = "手机号码不正确！！";
			resObject.put("resMsg", rerro);
		}else if(!isDate){
			rerro = "出生日期格式不正确！！";
			resObject.put("resMsg", rerro);
		}else{
			res = userService.insertUser(user);
		}
		resObject.put("res", res);
		return resObject;
		
	}
	
	@RequestMapping("getUser")
	@ResponseBody
	public User getUser(){
		User user = new User();
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		String userName = session.getAttribute("userName").toString();
		user = userService.findByName(userName);
		return user;
	}
	
	@RequestMapping("upDateUser")
	@ResponseBody
	public JSONObject upDateUser(@RequestBody User user){
		JSONObject resObject = new JSONObject();
		int res = 0;String rerro = "";
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		String userId = (String)session.getAttribute("userId");
		user.setId(userId);
		if(user.getPassword() != null){
			boolean isPwd = RegexUtil.isPwd(user.getPassword());
			if(!isPwd){
				rerro = "密码格式不正确！";
				resObject.put("resMsg", rerro);
			}else{
				res = userService.upDateUser(user);
			}
		}else{
			res = userService.upDateUser(user);
		}
		
		resObject.put("res", res);
		return resObject;
		
	}
	
	//修改头像
	@RequestMapping("editPic")
	@ResponseBody
	public JSONObject uploadFile(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("file") CommonsMultipartFile file) throws IOException {
		JSONObject resMsg = new JSONObject();
		User user = new User();
		String picPath = "";
		String msg = "";
		long setFileSizeMax = 2097152L;//定义最大文件为3M，字节形式
		long fileSize = file.getSize();//获取文件大小，字节形式
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		String userName = (String)session.getAttribute("userinfo");
		String userId = (String)session.getAttribute("userId");
		user.setId(userId);
		user.setName(userName);
		
		response.setContentType("text/html");
		response.setCharacterEncoding("GBK");
		Map<String,String> flag = new HashMap<String, String>();
		
		if(fileSize > setFileSizeMax){
			msg = "文件超过了3M！";
			resMsg.put("res", "0");
			resMsg.put("msg", msg);
		}else{
			if (file.getSize() > 0) {
				//文件上传的位置可以自定义 
				flag = FileUploadUtil.upLoadFile(request,file);			
			}
			if (flag.get("upRes").equals("true")) {
				picPath = flag.get("picPath");
				resMsg.put("picPath", picPath);
				user.setPic(picPath);
				int upRes = userService.upDateUser(user);
				resMsg.put("res", upRes);
			} else {
				resMsg.put("res", "0");
			}
		}
		return resMsg;
	}
}
